<?php
  
  class Language
  {
      protected $lang_;
      private $lang_path;
      public $DDPRO_lang;
      private $langdir;
      
      /**
       * Language::__construct()
       *
       * @return
       */
      public function __construct()
	  {
		  $this->langdir = DDPATH . "lang/";
		  
		  if (isset($_GET['lang']) || isset($_COOKIE['DDPROlanguage'])) {
			  if (isset($_GET['lang'])) {
				  $this->DDPRO_lang = sanitize($_GET['lang'], 2);
			  } else {
				  $this->DDPRO_lang = sanitize($_COOKIE['DDPROlanguage'], 2);
			  }
			  if (@file_exists($this->langdir . $this->DDPRO_lang . '.lang.php')) {
				  setcookie('DDPROlanguage', $this->DDPRO_lang, time() + COOKIE_EXPIRE);
				  $this->lang_path = $this->langdir . $this->DDPRO_lang . '.lang.php';
				  $this->lang_ = include($this->lang_path);
			  } else {
				  $this->DDPRO_lang = $this->getDefaultLang();
				  $this->lang_path = $this->langdir . $this->DDPRO_lang . '.lang.php';
				  
				  $this->lang_ = include($this->lang_path);
			  }
		  } else {
			  $this->DDPRO_lang = $this->getDefaultLang();
			  $this->lang_path = $this->langdir . $this->DDPRO_lang . '.lang.php';
			  
			  $this->lang_ = include($this->lang_path);
		  }
	  }
      
      /**
       * Language::getDefaultLang()
       *
       * @return
       */
      protected function getDefaultLang()
      {
          global $setup;
          return $setup->set['site_lang'];
      }
      
      /**
       * Language::getLangPhrase()
       *
       * @param mixed $string
       * @return string
       */
      public function getLangPhrase($string)
      {
          return $this->lang_[$string];
      }
      
      /**
       * Language::getLanguage()
       *
       * @return
       */
      public function getLanguage()
      {
          checkDir($this->langdir);
          $handle = opendir($this->langdir);
          $langicons = '';
          while (false !== ($file = readdir($handle))) {
              $langISO = substr($file, 0, 2);
              if ($file != "." && $file != ".." && $file != "_notes" && $file != "index.php" && $file != "blank.png") {
                  if ($this->DDPRO_lang == $langISO) {
                      $langicons .= '<img src="' . THEMEURL . '/images/flags/' . $langISO . '.png" class="active" alt="' . $langISO . '"/>';
                  } else
                      $langicons .= '<a href="' . SITEURL . '/index.php?lang=' . $langISO . '"><img src="' . THEMEURL . '/images/flags/' . $langISO . '.png" alt="' . $langISO . '"/></a>';
              }
          }
          closedir($handle);
          
          return $langicons;
      }
  }
  
  /**
   * lang()
   * returns the translation for the phrase
   * @param mixed $string
   * @return
   */
  function lang($string)
  {
      global $lng;
      return $lng->getLangPhrase($string);
  }
?>